package com.abaaba.project2.controller;

import com.abaaba.project2.bean.Groupon;
import com.abaaba.project2.bean.GrouponRules;
import com.abaaba.project2.bean.bo.GrouponReqBo;
import com.abaaba.project2.bean.bo.GrouponRuleCreateBo;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.service.AdminGrouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/groupon")
public class AdminGrouponController {

    @Autowired
    AdminGrouponService adminGrouponService;

    @RequestMapping("list")
    public BaseRespVo list(GrouponReqBo grouponReqBo) {
        BaseData<GrouponRules> grouponRulesBaseData = adminGrouponService.queryGrouponRuleList(grouponReqBo);
        return BaseRespVo.ok(grouponRulesBaseData);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody GrouponRuleCreateBo createBo) {
        GrouponRules grouponRules = adminGrouponService.insertGrouponRule(createBo);
        return BaseRespVo.ok(grouponRules);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody GrouponRules grouponRules) {
        try {
            adminGrouponService.updateGrouponRule(grouponRules);
        }catch (Exception e){
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody  GrouponRules grouponRules) {
        try {
            adminGrouponService.deleteGrouponRule(grouponRules);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("listRecord")
    public BaseRespVo listRecord(GrouponReqBo grouponReqBo) {
        BaseData<Groupon> grouponBaseData = adminGrouponService.queryGrouponList(grouponReqBo);
        return BaseRespVo.ok(grouponBaseData);
    }
}
